.PHONY:clear

NAME:= beep
ALL:= $(NAME).bin

CRSS_COMPILE :=arm-linux-gnueabihf-

CC:=$(CRSS_COMPILE)gcc
LD:=$(CRSS_COMPILE)ld
OBJCOPY:=$(CRSS_COMPILE)objcopy
OBJDUMP:=$(CRSS_COMPILE)objdump

INCDIRS:= 	imx6ul \
			project \
			bsp/clk \
			bsp/led \
			bsp/beep \
			bsp/delay 

SRCDIRS:= 	project \
			bsp/clk \
			bsp/led \
			bsp/beep \
			bsp/delay 
			
#INCDIRS:= $(shell find $(INCDIRS)  -name '*.h')
CFILES:= $(shell find $(SRCDIRS)  -name '*.c')
SFILES:= $(shell find $(SRCDIRS)  -name '*.s')

INCLUDE:=$(patsubst %,-I %, $(INCDIRS))

SFILENDIR:=$(notdir $(SFILES))
CFILENDIR:=$(notdir $(CFILES))

SOBJS:=$(patsubst %, obj/%,$(SFILENDIR:%.s=%.o))
COBJS:=$(patsubst %, obj/%,$(CFILENDIR:%.c=%.o))

OBJS:=$(SOBJS)$(COBJS)

VPATH:=$(SRCDIRS)

$(ALL):$(NAME)
	$(OBJCOPY) -O binary -S  $?.lef $@
	$(OBJDUMP) -D -m arm $?.lef > $?.dis

$(NAME):$(OBJS)
	$(LD) -Timx6ul.lds $^ -o $@.lef

$(SOBJS):obj/%.o:%.s
	$(CC) -Wall -nostdlib -c -O2 $(INCLUDE) -o $@ $<

$(COBJS):obj/%.o:%.c
	$(CC) -Wall -nostdlib -c -O2 $(INCLUDE) -o $@ $<

imx:$(ALL)
	./imxdownload $^ ./imx 

clear:
	rm -rf $(OBJS) $(ALL) $(NAME).lef $(NAME).dis  imx
